package io.renren.modules.yckj.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.renren.common.page.PageData;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.modules.yckj.dao.MajorsDao;
import io.renren.modules.yckj.dto.MajorsDTO;
import io.renren.modules.yckj.entity.MajorsEntity;
import io.renren.modules.yckj.service.MajorsService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2025-06-13
 */
@Service
public class MajorsServiceImpl extends CrudServiceImpl<MajorsDao, MajorsEntity, MajorsDTO> implements MajorsService {

    @Override
    public QueryWrapper<MajorsEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");
        String schoolName = (String) params.get("schoolName");
        String key = (String) params.get("key");

        QueryWrapper<MajorsEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(id), "id", id);
        wrapper.eq(StrUtil.isNotBlank(schoolName), "school_name", schoolName);
        wrapper.like(StrUtil.isNotBlank(key), "school_name", key);

        Boolean groupBy = (Boolean) params.get("groupBy");
        if (groupBy != null && groupBy)
            wrapper.groupBy("school_name");

        return wrapper;
    }

    @Override
    public PageData<MajorsDTO> pageForApi(Map<String, Object> params) {
        //分页
        IPage<MajorsEntity> page = getPage(params, null, false);
        //查询
        List<MajorsEntity> list = baseDao.getSchoolGroupBy(params);
        return getPageData(list, page.getTotal(), MajorsDTO.class);
    }
}